/**
 * 添加或者修改页面
 */
var PinChannelInfoDlg = {
    data: {
        apiUrl: "",
        apiType: "",
        params: "",
        createTime: "",
        updateTime: "",
        deleteFlag: "",
        headers: "",
        remark: "",
        channelName: "",
        signType: "",
        channelCode: "",
        signCaps: "",
        paramSignType: "",
        categoryId: ""
    }
};

layui.use(['form','table', 'admin', 'ax'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var table = layui.table;
    var admin = layui.admin;

    //让当前iframe弹层高度适应
    admin.iframeAuto();

    var PinChannelParam = {
        tableId: "channelParamsTable"
    };

    PinChannelParam.initColumn = function () {
        return [[
            {field: 'name', sort: true, title: '参数名称', edit: 'text'},
            {field: 'key', sort: true, title: '参数键名称', edit: 'text'},
            {field: 'value', sort: true, title: '参数值', edit: 'text'},
            {align: 'center', toolbar: '#paramTableBar', title: '操作'}
        ]];
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + PinChannelParam.tableId,
        page: true,
        height: "full-158",
        toolbar: '#paramToolbar',
        cellMinWidth: 100,
        limits:10000,
        page:false,
        data:[{}],
        cols: PinChannelParam.initColumn()
    });

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/pinChannel/addItem", function (data) {
            Feng.success("添加成功！");
            window.location.href = Feng.ctxPath + '/pinChannel?categoryId=' + $("#categoryId").val();
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        var taData = table.cache['' + PinChannelParam.tableId];
        data.field.params = JSON.stringify(taData);
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    //返回按钮
    $("#backupPage").click(function () {
        window.location.href = Feng.ctxPath + '/pinChannel?categoryId=' + $("#categoryId").val();
    });

    table.on('toolbar(' + PinChannelParam.tableId + ')', function(obj){
        var checkStatus = table.checkStatus(obj.config.id);
        var taData = table.cache['' + PinChannelParam.tableId];
        if (obj.event === 'insertTable') {
            taData.push({});
            table.reload('' + PinChannelParam.tableId,{
                data : taData
            });
        }
    });

    // 工具条点击事件
    table.on('tool(' + PinChannelParam.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'delete') {
            var taData = table.cache['' + PinChannelParam.tableId];
            taData.splice(jQuery.inArray(obj.data, taData), 1);
            table.reload('' + PinChannelParam.tableId,{
                data : taData
            });
        }
    });

});
